Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Opcodes and Errors classes in generated Typescript code #1100

Closed
wants to merge 4 commits into from

Conversation

Shvandre
Copy link
Contributor

@Shvandre Shvandre commented Nov 28, 2024

Issue

Closes #1033 .

Checklist

  • I have updated CHANGELOG.md
  • I have documented my contribution in docs/ and made the build locally
  • I have added tests to demonstrate the contribution is correctly implemented: this usually includes both positive and negative tests, showing the happy path(s) and featuring intentionally broken cases
  • I have run all the tests locally and no test failure was reported
  • I have run the linter, formatter and spellchecker
  • I did not do unrelated and/or undiscussed refactorings

…Added opcodes class generation. Added ExitCodes map generation.
@Shvandre Shvandre requested a review from a team as a code owner November 28, 2024 09:02
@Shvandre Shvandre self-assigned this Nov 28, 2024
@anton-trunov anton-trunov changed the title Added Opcodes and Errors classes in generated Typescript code. Add Opcodes and Errors classes in generated Typescript code Nov 28, 2024
@anton-trunov anton-trunov added the scope: ts-wrappers TypeScript wrappers for Sandbox tests label Nov 28, 2024
@anton-trunov anton-trunov added this to the v1.6.0 milestone Nov 28, 2024
Copy link
Member

@anton-trunov anton-trunov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's either

  • add tests with the new functionality, or
  • simplify the existing tests using it

@anton-trunov
Copy link
Member

image

apparently this is not entirely true :)
CI is red because Prettier is unhappy


//Opcodes
//So user can use them in sandbox tests
w.append("export abstract class Opcodes {");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TypeScript has enum syntax for this. abstract class implies user should inherit from it to get access to its fields, and that's definitely not the way in a language without multiple inheritance.

@@ -272,58 +303,66 @@ export function writeTypescript(

if (init) {
w.append(
`static async init(${writeArguments(init.args).join(", ")}) {`,
`static async init<T extends ${abi.name}>(` +
`this: new (address: Address, init?: { code: Cell, data: Cell }) => T,` +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This kind of types is meant to type legacy JS code, not to write or generate novel code.

w.append(`const ${abi.name}_errorMessages: { [key: string]: number } = {`);
w.inIndent(() => {
if (abi.errors) {
Object.entries(abi.errors).forEach(([k, abiError]) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for (const [k, abiError] of abi.errors) would be more readable, concise and performant.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@verytactical there is a few of instances of Object.entries(foo).forEach(([k, v]) ... in the codebase. Could you please open an issue to refactor those?

if (abi.errors) {
Object.entries(abi.errors).forEach(([k, abiError]) => {
const escapedMessage = abiError.message.replaceAll('"', '\\"');
w.append(` "${escapedMessage}": ${k},`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an indent that doesn't take .inIndent() state in consideration.

@anton-trunov
Copy link
Member

Let's move on with this PR please. @Shvandre can we see some tests for the changes?

@Shvandre
Copy link
Contributor Author

Shvandre commented Dec 11, 2024

Decided to put this into the backlog. Will return to this after Typescript codegen refactoring

@Shvandre Shvandre closed this Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope: ts-wrappers TypeScript wrappers for Sandbox tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enum "Opcodes" and "Errors" are possibly missing in generated .ts file
3 participants